package com.yswl.yeeduo.ui.activity;

import java.util.Map;
import java.util.TreeMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.App;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.bean.ServeiceAuthBean;
import com.yswl.yeeduo.ui.fragment.LuckyMoneyActivityFragment;
import com.yswl.yeeduo.ui.fragment.MyMoneyFragment;
import com.yswl.yeeduo.ui.task.LoginTask2;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.ui.tool.UserManger;

public class LoginActivity extends BaseActivity implements OnClickListener, TaskDelegate {

	private static final int LOGIN_ID = 11001;
	private EditText mTelNumber;
	private EditText mPwd;

	private String userName;
	private String userPwd;
	private Button mLogin;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_login);
		setHeader("登录");

		mTelNumber = (EditText) findViewById(R.id.tel_nmb_et);
		mPwd = (EditText) findViewById(R.id.pwd_et);

		TextView mReg = (TextView) findViewById(R.id.reg_tv);
		mReg.setOnClickListener(this);

		mLogin = (Button) findViewById(R.id.login);
		mLogin.setOnClickListener(this);

		TextView mFoundPwd = (TextView) findViewById(R.id.forgit_pwd);
		mFoundPwd.setOnClickListener(this);

		

	}

	// Um
	public void onResume() {
		super.onResume();
		
		String name = UserManger.getInstance().getUserName(this);
		if (KStringTool.isNotEmpty(name)) {
			mTelNumber.setText(name);
		}
		
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
		
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}

	@Override
	public void onClick(View v) {
		userName = mTelNumber.getText().toString().trim();
		userPwd = mPwd.getText().toString().trim();
		switch (v.getId()) {
		case R.id.forgit_pwd:
			startActivity(new Intent(this, ForgetPwdActivity.class));
			break;
		case R.id.reg_tv:
			startActivity(new Intent(this, RegisterActivity.class));
			break;
		case R.id.login:
			if (KStringTool.isNotEmpty(userName) && KStringTool.isNotEmpty(userPwd)) {
				Map<String, String> map = new TreeMap<String, String>();
				map.put("api_key", ServiceAuthManager.getToken(this));
				map.put("phone", userName);
				map.put("passwd", userPwd);
				new LoginTask2(LoginActivity.this, LOGIN_ID, this).execute(map);
			}
			break;
		default:
			break;
		}

	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (taskId == LOGIN_ID) {
			Toast.makeText(this, "用户不存在或密码不正确", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		if (taskId == LOGIN_ID) {
			UserManger.getInstance().recordUserName(this, userName);// 登录后记录用户名字
			ServeiceAuthBean user = (ServeiceAuthBean) object;
			App.getInstance().isLogin = true;
			if (user == null) {
				ToastUtil.showLongMsg("登录失败，请重试");
				return;
			}
			ServiceAuthManager.saveAuthParams(this, user.token, user.secret, user.userid);

			new  Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					// 登录成功 发送广播通知刷新活动列表
					LuckyMoneyActivityFragment.sendBroadcast(LoginActivity.this);
					finish();
				}
			}, 200);
			
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// exitApp();
			finish();
		}
		return true;
	}

}
